<%-- 
    Document   : index
    Created on : Feb 18, 2012, 2:28:54 PM
    Author     : Truong
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
<script type="text/javascript">
function loadXMLDoc(dname)
{

if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function createCategory() {
xml=loadXMLDoc("xml/Categories.xml");
path="//category_list/category"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
  {
     option = document.createElement("option");     
  option.setAttribute("value", nodes[i].childNodes[0].childNodes[0].nodeValue);
  option.innerHTML = nodes[i].childNodes[1].childNodes[0].nodeValue.toString();
   document.getElementById("category").appendChild(option);
  
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
 var option;
while (result)
  {
    
       option = document.createElement("option");
  option.setAttribute("value", result.childNodes[0].childNodes[0].nodeValue);
  option.innerHTML = result.childNodes[1].childNodes[0].nodeValue.toString();
  select.appendChild(option);
   document.getElementById("category").appendChild(option);

  result=nodes.iterateNext();
  }
}
function setCategoryId(id) {
    document.getElementById(id)
}
}
</script>
        <form action="index.jsp" name="myForm">
             <c:import url="xml/Categories.xml" var="xmlCat"/>
           <x:parse var="doc" xml="${xmlCat}" />
            <table align="center" class="search">
                <tr>
                    <td>
                        Name <input class="text" type="text" name="txtName" value="" />
                    </td>
                    <td>
                        <select id="category" name="category" size="1">
                            <option selected value="all" >Select by Category...</option>
                           
                        </select>

                            <script type="text/javascript">
                                createCategory();
                            </script>
                                
                    </td>
                    <td>
                        <select name ="priceFrom" size="1">
                            <option selected value="0">Price From...</option>
                            <option value="50">$50</option>
                            <option value="100">$100</option>
                            <option value="200">$200</option>
                            <option value="500">$500</option>
                        </select>
                    </td>
                    <td>
                        <select name ="priceTo" size="1">
                            <option selected value="9999999">Price To...</option>
                            <option value="99">$99</option>
                            <option value="199">$199</option>
                            <option value="299">$299</option>
                            <option value="999">$999</option>
                        </select>
                    </td>
                    <td>
                        <input type="submit" value="Search" class="searchButton" />
                    </td>
                </tr>
            </table>
        </form>
       
        <c:set  var="search" value="${param.txtName}" />
        <c:set var="category" value="${param.category}" />
        <c:set var="priceFrom" value="${param.priceFrom}" />
        <c:set var="priceTo" value="${param.priceTo}" />

    <%--    <c:if test="${not empty search}">
  <c:import url="xml/Products.xml" var="xmlPro"/>
           <x:parse var="doc2" xml="${xmlPro}" />
            
            <c:if test="${category != 'all'}">
                <x:forEach var="node"
                           select="$doc2//product_lists/product[contains(name,$search) and contains(category, $category) and price >= $priceFrom and price <= $priceTo]">

                    Details: <br />
                    <table border="1">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Product Name</th>
                                <th>Product Detail</th>
                                <th>Product Price</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:set var="count" value="0"/>
                            <c:set var="count" value="${count +1}" />
                            <tr>
                                <td>${count}</td>
                                <td><x:out select="$node/name"/></td>
                                <td><x:out select="$node/detail"/></td>
                                <td><x:out select="$node/price"/></td>
                            </tr>
                        </tbody>
                    </table>



                </x:forEach>
            </c:if>
            <c:if test="${category == 'all'}">
                <x:forEach var="node"
                           select="$doc2//product_lists/product[contains(name,$search)]">

                    Details: <br />
                    <table border="1">
                        <thead>
                            <tr>
                                <th>No.</th>
                                <th>Product Name</th>
                                <th>Product Detail</th>
                                <th>Product Price</th>
                            </tr>
                        </thead>
                        <tbody>
                            <c:set var="count" value="0"/>
                            <c:set var="count" value="${count +1}" />
                            <tr>
                                <td>${count}</td>
                                <td><x:out select="$node/name"/></td>
                                <td><x:out select="$node/detail"/></td>
                                <td><x:out select="$node/price"/></td>
                            </tr>
                        </tbody>
                    </table>
                </x:forEach>
            </c:if>

        </c:if> --%>
    </body>
</html>

